﻿using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using Business;

namespace ServicesDirectory.UserControl
{
    public partial class ListOrganisationService : System.Web.UI.UserControl
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            const string draggable = "$( '#draggable' ).draggable();";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), UpdatePanel1.ClientID, draggable, true);
            
            Repeater1.ItemCommand += Repeater1_ItemCommand;
            Repeater1.ItemDataBound += Repeater1_ItemDataBound;
            LookupRole1.CloseControl += LookupRole1_CloseControl;
            LookupRole1.GetRoles += LookupRole1_GetRoles;
            cmdFirst.Click += CmdFirst_Click;
            cmdNext.Click += CmdNext_Click;
            cmdBack.Click += CmdBack_Click;
            cmdLast.Click += CmdLast_Click;
            Display();
        }

        /// <summary>
        /// Handles the GetRoles event of the LookupRole1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void LookupRole1_GetRoles(object sender, EventArgs e)
        {
            OrganizationBusiness.Instance.EditRoles(new Guid(ViewState["orgId"].ToString()), sender as Collection<int>);
            Display();
        }

        #region paging button
        protected void CmdFirst_Click(object sender, EventArgs e)
        {
            txtPageNumber.Text = "1";
            Display();
        }

        protected void CmdBack_Click(object sender, EventArgs e)
        {
            if (Int32.Parse(txtPageNumber.Text, CultureInfo.CurrentCulture) > 1)
            {
                txtPageNumber.Text = (Int32.Parse(txtPageNumber.Text, CultureInfo.CurrentCulture) - 1).ToString(CultureInfo.CurrentCulture);
            }
            Display();
        }

        protected void CmdNext_Click(object sender, EventArgs e)
        {
            if (Int32.Parse(txtPageNumber.Text, CultureInfo.CurrentCulture) < Int32.Parse(lblTotalPage.Text, CultureInfo.CurrentCulture))
            {
                txtPageNumber.Text =
                    (Int32.Parse(txtPageNumber.Text, CultureInfo.CurrentCulture) + 1).ToString(
                        CultureInfo.CurrentCulture);
            }
            Display();
        }

        protected void CmdLast_Click(object sender, EventArgs e)
        {
            txtPageNumber.Text = lblTotalPage.Text;
            Display();
        }
        #endregion

        /// <summary>
        /// Handles the CloseControl event of the LookupRole1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void LookupRole1_CloseControl(object sender, EventArgs e)
        {
            pnLookupRole.Visible = false;
        }

        /// <summary>
        /// Handles the ItemDataBound event of the Repeater1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RepeaterItemEventArgs" /> instance containing the event data.</param>
        private void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var lblRoles = ((Label)e.Item.FindControl("lblRoles"));
            var arrayRoles = ((HiddenField) e.Item.FindControl("hdfArrayRoles")).Value;
            var textRoles = RoleBusiness.Instance.CreateRoles(arrayRoles);
            lblRoles.Text = textRoles;
        }

        /// <summary>
        /// Handles the ItemCommand event of the Repeater1 control.
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="e">The <see cref="RepeaterCommandEventArgs" /> instance containing the event data.</param>
        private void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if(e.CommandName == "EditRole")
            {
                ViewState["orgId"] = ((HiddenField) e.Item.FindControl("hdfOrgId")).Value;
                var roles = ((HiddenField)e.Item.FindControl("hdfArrayRoles")).Value;
                LookupRole1.BindData(roles);
                pnLookupRole.Visible = true;
            }
        }

        /// <summary>
        /// Displays this instance.
        /// </summary>
        private void Display()
        {
            var listOrganisations = OrganizationBusiness.Instance.ListOrganisation("All", true);
            listOrganisations = Paging(listOrganisations);
            Repeater1.DataSource = listOrganisations;
            Repeater1.DataBind();
        }

        /// <summary>
        /// Pagings the specified list organisations.
        /// </summary>
        /// <param name="listOrganisations">The list organisations.</param>
        /// <returns></returns>
        private Collection<Organisation> Paging(Collection<Organisation> listOrganisations)
        {
            lblTotalPage.Text = (listOrganisations.Count % 8 == 0 ? listOrganisations.Count / 8 : listOrganisations.Count / 8 + 1).ToString(CultureInfo.InvariantCulture);
            
            var pagedList = OrganizationBusiness.Instance.PagingOrg(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 8, listOrganisations);

            return pagedList;
        }
    }
}